#!/bin/bash
# Copyright (C) 2015 Yaowen HE
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

tag="@tag@"

prefix="@prefix@"

bindir="$prefix/bin"
confdir="$prefix/etc"
sitedir="$prefix/share/fmark"

source "$sitedir"/functions.sh

# display help information
# usage: display_help
display_help () {
    echo "usage: fmark <command> [options] [args]"
    echo "Bookmark manager for Mozilla Firefox"
    echo
    echo "Available sub-commands:"

    for cmd in $(ls -1 "$sitedir"/fmark-* 2> /dev/null); do
        basename $cmd | sed 's/fmark-\(.*\)\.sh/  \1/'
    done

    echo "  help"
}

if has_opt "version" "$@"; then
    echo $tag
    exit
fi

cmd=$(get_cmd "$@")

if [ -z $cmd ]; then
    cmd="help"
fi

if [ "$cmd" = "help" ]; then
    display_help
    exit
fi

if [ ! -r "$sitedir"/fmark-$cmd.sh ]; then
    echo "'$cmd' is not an available command" >&2
    exit 1
fi

source "$sitedir"/fmark-$cmd.sh "$@"
